package com.amanda.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.amanda.constant.MessageConstant;
import com.amanda.entity.PageResult;
import com.amanda.entity.QueryPageBean;
import com.amanda.entity.Result;
import com.amanda.pojo.CheckGroup;
import com.amanda.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import static com.amanda.constant.MessageConstant.*;

/**
 * @author amanda
 * @Description 检查组服务
 */

@RequestMapping("/checkgroup")
@RestController
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            checkGroupService.add(checkGroup,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return checkGroupService.pageQuery(queryPageBean);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try{
            checkGroupService.edit(checkGroup, checkitemIds);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, EDIT_CHECKGROUP_SUCCESS);
    }

    // 回显数据
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            CheckGroup checkGroup = checkGroupService.findById(id);
            return new Result(true,QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,QUERY_CHECKGROUP_FAIL);
        }
    }

    // 根据检查组ID查询当前检查组包含的检查项ID，用于页面复选框回显
    // List<Integer>
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        try {
            List<Integer> list = checkGroupService.findCheckItemIdsByCheckGroupId(id);
            return new Result(true,QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/deleteGroupAndItem")
    public Result deleteGroupAndItem(Integer id) {
        try {
            checkGroupService.delete(id);
        }catch (Exception e) {
            e.printStackTrace();
            // 服务调用失败
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true, DELETE_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<CheckGroup> list = checkGroupService.findAll();
            return new Result(true, QUERY_CHECKGROUP_SUCCESS, list);
        }catch (Exception e) {
            e.printStackTrace();
            // 服务调用失败
            return new Result(false, QUERY_CHECKGROUP_FAIL);
        }
    }
}
